package me.luger.project.fof.controller.admin;

import me.luger.core.dao.nutz.entity.page.DataTableRequest;
import me.luger.core.mvc.controller.BaseController;
import me.luger.core.mvc.entity.Result;
import me.luger.core.mvc.interceptor.oplog.OpLog;
import me.luger.project.fof.service.AssetStrategyInfoService;
import me.luger.project.fof.service.BenchmarkService;
import me.luger.project.fof.entity.AssetStrategyInfo;
import me.luger.project.fof.entity.Benchmark;
import org.nutz.dao.Sqls;
import org.nutz.dao.sql.Sql;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

/**
 * Benchmark controller
 */
@Controller
@RequestMapping("/private/fof/benchmark")
public class BenchmarkController extends BaseController {

    private String viewBasePath = "/admin/modules/benchmark/";

    @Autowired
    private BenchmarkService benchmarkService;

    @Autowired
    private AssetStrategyInfoService assetStrategyInfoService;

    @RequestMapping(value = "",name = "Benchmark列表页面")
    public ModelAndView index(){
        return btlView(viewBasePath + "index");
    }

    @RequestMapping(value = "data",name = "获取Benchmark列表")
    @ResponseBody
    public Object data(@RequestBody final DataTableRequest dataTableRequest){
        Sql sql = Sqls.create("select b.id,b.name,b.windCode,a.name as assetStrategyName from fof_benchmark b left join fof_asset_strategy_info a on b.assetStrategyId = a.id $condition");
        return benchmarkService.data(dataTableRequest,sql);
    }



    @RequestMapping(value = "delete",name = "删除Benchmark接口")
    @ResponseBody
    public Object delete(String[] ids){
        benchmarkService.delete(ids);
        return Result.success("delete success");
    }

    @RequestMapping(value = "/detail/{id}",name = "获取Benchmark详细信息接口")
    public ModelAndView detail(@PathVariable("id") Integer id){
        Benchmark benchmark = benchmarkService.fetchById(id);
        setAttr("benchmark",benchmark);
        return btlView(viewBasePath + "detail");
    }

    @RequestMapping(value = "add",name = "添加Benchmark页面")
    public ModelAndView add(){
        List<AssetStrategyInfo> assetInfoList = assetStrategyInfoService.getAssetStrategyInfoByType(1);
        List<AssetStrategyInfo> strategyInfoList = assetStrategyInfoService.getAssetStrategyInfoByType(2);

        setAttr("assetInfoList",assetInfoList);
        setAttr("strategyInfoList",strategyInfoList);
        return btlView(viewBasePath + "add");
    }

    @RequestMapping(value = "addDo",name = "添加Benchmark接口")
    @ResponseBody
    @OpLog(name = "添加Benchmark",msg = "添加Benchmark")
    public Object adddo(Benchmark benchmark){
        try{
            String[] assetStrategyArr = request.getParameterValues("assetStrategyArr");
            if(assetStrategyArr != null && assetStrategyArr.length == 1){
                int assetStrategyId = Integer.parseInt(assetStrategyArr[0]);
                benchmark.setAssetStrategyId(assetStrategyId);

                AssetStrategyInfo assetStrategyInfo = assetStrategyInfoService.fetchById(assetStrategyId);
                benchmark.setTypeId(assetStrategyInfo.getType());

            }
            benchmark = benchmarkService.insert(benchmark);
        }catch (Exception e){
            return Result.error();
        }
        return Result.success();
    }

    @RequestMapping(value = "/edit/{id}",name = "更新Benchmark信息页面")
    public ModelAndView edit(@PathVariable Integer id){
        Benchmark benchmark = benchmarkService.fetchById(id);
        setAttr("benchmark",benchmark);

        List<AssetStrategyInfo> assetInfoList = assetStrategyInfoService.getAssetStrategyInfoByType(1);
        List<AssetStrategyInfo> strategyInfoList = assetStrategyInfoService.getAssetStrategyInfoByType(2);

        setAttr("assetInfoList",assetInfoList);
        setAttr("strategyInfoList",strategyInfoList);

        return btlView(viewBasePath + "edit");
    }

    @RequestMapping(value = "editDo",name = "更新Benchmark接口")
    @ResponseBody
    public Object editDo(Benchmark benchmark){
        try{
            String[] assetStrategyArr = request.getParameterValues("assetStrategyArr");
            if(assetStrategyArr != null && assetStrategyArr.length == 1){
                int assetStrategyId = Integer.parseInt(assetStrategyArr[0]);
                benchmark.setAssetStrategyId(assetStrategyId);

                AssetStrategyInfo assetStrategyInfo = assetStrategyInfoService.fetchById(assetStrategyId);
                benchmark.setTypeId(assetStrategyInfo.getType());
            }
            benchmarkService.updateIgnoreNull(benchmark);
        }catch (Exception e){
            return Result.error();
        }
        return Result.success();
    }
}
